package server

import (
	"embed"
	"io/fs"
	"net/http"
	"strings"
)

//--go:embed ../frontend/dist
// var content embed.FS

// SetupStaticFileServer 设置静态文件服务
func SetupStaticFileServer(content embed.FS) http.Handler {
	distFS, err := fs.Sub(content, "web")
	if err != nil {
		panic(err)
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// 如果请求路径是根路径，则默认返回index.html
		path := r.URL.Path
		if path == "/" {
			path = "/index.html"
		}

		// 去除开头的斜杠，因为fs.Sub已经提供了正确的根路径
		path = strings.TrimPrefix(path, "/")

		// 尝试提供静态文件
		http.FileServer(http.FS(distFS)).ServeHTTP(w, r)
	})
}
